package avicit.bdp.dms.oauth2;

import org.apache.commons.lang.StringUtils;
import org.apache.oltu.oauth2.common.message.OAuthResponse;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.Map;

/**
 * TODO
 *
 * @author xugb
 * @date 2024/4/12 15:09
 */

public class WebUtils {
    private WebUtils() {
    }

    public static void writeOAuthJsonResponse(HttpServletResponse response, OAuthResponse oAuthResponse) {
        int responseStatus = oAuthResponse.getResponseStatus();

        try {
            Map<String, String> headers = oAuthResponse.getHeaders();
            Iterator var4 = headers.keySet().iterator();

            while(var4.hasNext()) {
                String key = (String)var4.next();
                response.addHeader(key, (String)headers.get(key));
            }

            response.setHeader("Access-Control-Allow-Origin", "*");
            response.setContentType("application/json;charset=UTF-8");
            response.setStatus(responseStatus);
            PrintWriter out = response.getWriter();
            out.print(oAuthResponse.getBody());
            out.flush();
        } catch (IOException var6) {
            throw new IllegalStateException("Write OAuthResponse error", var6);
        }
    }

    public static void writeCheckTokenResponse(HttpServletResponse response, OAuthResponse oAuthResponse) {
        int responseStatus = oAuthResponse.getResponseStatus();

        try {
            Map<String, String> headers = oAuthResponse.getHeaders();
            Iterator var4 = headers.keySet().iterator();

            while(var4.hasNext()) {
                String key = (String)var4.next();
                response.addHeader(key, (String)headers.get(key));
            }

            response.setHeader("Access-Control-Allow-Origin", "*");
            response.setContentType("application/json;charset=UTF-8");
            response.setStatus(responseStatus);
            PrintWriter out = response.getWriter();
            out.print(oAuthResponse.getBody());
            out.flush();
        } catch (IOException var6) {
            throw new IllegalStateException("Write OAuthResponse error", var6);
        }
    }

    public static void writeOAuthQueryResponse(HttpServletResponse response, OAuthResponse oAuthResponse) {
        String locationUri = oAuthResponse.getLocationUri();

        try {
            Map<String, String> headers = oAuthResponse.getHeaders();
            Iterator var4 = headers.keySet().iterator();

            while(var4.hasNext()) {
                String key = (String)var4.next();
                response.addHeader(key, (String)headers.get(key));
            }

            response.setStatus(oAuthResponse.getResponseStatus());
            response.sendRedirect(locationUri);
        } catch (IOException var6) {
            throw new IllegalStateException("Write OAuthResponse error", var6);
        }
    }

    public static String retrieveClientIp(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (isUnAvailableIp(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }

        if (isUnAvailableIp(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }

        if (isUnAvailableIp(ip)) {
            ip = request.getRemoteAddr();
        }

        return ip;
    }

    private static boolean isUnAvailableIp(String ip) {
        return StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip);
    }
}